﻿using UnityEngine;
public class DamageInfo
{
    public float damage;
    public float buffDamage;
    public Vector3 hitPos;
    public Transform hitPart;
    public object[] oherParam;//可选参数
    public DamageInfo(float damage, Vector3 hitPos, Transform hitPart, params object[] objects)
    {
        this.damage = damage;
        this.hitPos = hitPos;
        this.hitPart = hitPart;
        oherParam = objects;
    }
    
}
public class DamageInfoForPlay
{
    public float damage;
    public string hitName;
}

public class HealthBase<T>: IHealth<T> where T : MonoBehaviour
{
    public BindableProperty<float> currentHP { get; set; } = new BindableProperty<float>();//当前血量
    public float maxHP { get; set; }//最大血量
    public DamageInfo damageInfo { get; set; }//封装的伤害类
    public T self { get ; set ; }

    public float currentDamageImmunityPercentage =0;//免伤百分比
    public HealthBase(T self,float maxHP)
    {
        this.self = self;
        this.maxHP = maxHP;
        currentHP.Value = maxHP;
        currentDamageImmunityPercentage = 0;
    }
    public virtual void Init()
    {
        GameRoot.Instance.eventService.AddEventListening(EventID.OnHitTarget, OnHitTarget);
    }
    public virtual void UnInit()
    {
        GameRoot.Instance.eventService?.RemoveEventListeningByTarget(this);
    }
    public virtual void OnDead()//敌人死亡调用
    {
    
    }
    public void OnHitTarget(object target, object arg2)
    {
        if (self.transform != (Transform)target)
        {
            return;
        }
        damageInfo = (DamageInfo)arg2;
        if (currentDamageImmunityPercentage != 0)
        {
            damageInfo.damage *= (1 - currentDamageImmunityPercentage);
        }
        //伤害计算
        DamageMath(damageInfo);
        
        //扣血
        TakeDamage(damageInfo);

        //特效
        HitEffect(damageInfo);
    }
    public void ChangedDamageImmunityPercentage(float targetDamagePercentage)
    {
        currentDamageImmunityPercentage = targetDamagePercentage;
    }
    protected virtual void DamageMath(DamageInfo damageInfo)
    {
       
    }

    protected virtual void HitEffect(DamageInfo damageInfo)
    {
       
    }
    public virtual void Update()
    {

    }
    protected virtual void TakeDamage(DamageInfo damageInfo)
    {
        TakeDamage(damageInfo.buffDamage,true);
    }
    public void TakeDamage(float damage,bool enterHurtState)
    {
        currentHP.Value = Mathf.Clamp(currentHP.Value -= damage, 0, maxHP);
    }
}